package com.ashlikun.glideutils

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import android.widget.ImageView.ScaleType
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.ashlikun.glideutils.GlideUtils.getImageView
import com.ashlikun.glideutils.GlideUtils.setScaleType
import com.ashlikun.glideutils.okhttp.ProgressListener
import com.ashlikun.glideutils.okhttp.ProgressManage
import com.bumptech.glide.TransitionOptions
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.target.ViewTarget

/**
 * @author　　: 李坤
 * 创建时间: 2018/8/28 14:44
 * 邮箱　　：496546144@qq.com
 *
 *
 * 功能介绍：
 */
class GlideLoad private constructor(val request: GlideRequests) {
    private var requestOptions: RequestOptions? = null
    private var transitionOptions: TransitionOptions<*, Drawable>? = null
    private var path: Any? = null
    private var isSetScaleType = false

    //错误图的缩放类型
    private var errorScaleType = GlideUtils.errorScaleType

    //占位图的缩放类型
    private var placeholderScaleType = GlideUtils.placeholderScaleType
    private var progressListener: ProgressListener? = null
    private val requestListener by lazy { ArrayList<RequestListener<Drawable>>() }
    fun errorScaleType(errorScaleType: ScaleType?): GlideLoad {
        this.errorScaleType = errorScaleType
        return this
    }

    fun placeholderScaleType(placeholderScaleType: ScaleType?): GlideLoad {
        this.placeholderScaleType = placeholderScaleType
        return this
    }

    fun isSetScaleType(setScaleType: Boolean): GlideLoad {
        isSetScaleType = setScaleType
        return this
    }

    fun options(requestOptions: RequestOptions?): GlideLoad {
        this.requestOptions = requestOptions
        return this
    }

    fun transition(transitionOptions: TransitionOptions<*, Drawable>?): GlideLoad {
        this.transitionOptions = transitionOptions
        return this
    }

    fun progressListener(progressListener: ProgressListener): GlideLoad {
        this.progressListener = progressListener
        return this
    }

    fun listener(listener: RequestListener<Drawable>): GlideLoad {
        return addListener(listener)
    }

    fun addListener(listener: RequestListener<Drawable>): GlideLoad {
        requestListener.add(listener)
        return this
    }

    fun load(path: Any?): GlideLoad {
        this.path = path ?: ""
        return this
    }

    fun show(imageView: ImageView): ViewTarget<ImageView, Drawable> {
        //设置缩放类型
        setScaleType(imageView, isSetScaleType)
        return builder().into(imageView)
    }

    fun show(view: Target<Drawable>): Target<Drawable> {
        val target = builder().into(view)
        setScaleType(getImageView(target), isSetScaleType)
        return target
    }

    /**
     * 构建一个GlideRequest 可以再次扩展
     *
     * @return
     */
    fun builder(): GlideRequest<Drawable> {
        var requestBuilder = request.load(path)
        if (progressListener != null) {
            ProgressManage.add(path, progressListener)
        }
        requestBuilder.listener(ProgressRequestListener(isSetScaleType, path, progressListener))
        if (requestListener != null) {
            for (listener in requestListener!!) {
                requestBuilder.addListener(listener)
            }
        }
        if (requestOptions != null) {
            requestBuilder = requestBuilder.apply(requestOptions!!)
        }
        if (transitionOptions != null) {
            requestBuilder = requestBuilder.transition(transitionOptions!!)
        }
        return requestBuilder
    }

    //    private val request: GlideRequest<Drawable>?
//        private get() {
//            if (activityF != null) {
//                return GlideApp.with(activityF!!).load(path)
//            } else if (activity != null) {
//                return GlideApp.with(activity!!).load(path)
//            } else if (fragment != null) {
//                return GlideApp.with(fragment!!).load(path)
//            } else if (context != null) {
//                return GlideApp.with(context!!).load(path)
//            } else if (imageView != null) {
//                return GlideApp.with(imageView!!).load(path)
//            }
//            return null
//        }
    companion object {
        fun with(activity: Activity) = GlideLoad(GlideApp.with(activity))

        fun with(imageView: ImageView) = GlideLoad(GlideApp.with(imageView))

        fun with(context: Context) = GlideLoad(GlideApp.with(context))

        fun with(activity: FragmentActivity) = GlideLoad(GlideApp.with(activity))

        fun with(fragment: Fragment) = GlideLoad(GlideApp.with(fragment))
    }
}
